package com.java110.mall.system.cmd.admin;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.CmdContextUtils;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.housekeeping.HousekeepingResourceDto;
import com.java110.dto.housekeeping.HousekeepingServDto;
import com.java110.dto.product.ProductDto;
import com.java110.dto.shop.ShopDto;
import com.java110.dto.store.StoreDto;
import com.java110.dto.storeOrder.StoreOrderDto;
import com.java110.intf.cart.IStoreOrderInnerServiceSMO;
import com.java110.intf.goods.IHousekeepingResourceInnerServiceSMO;
import com.java110.intf.goods.IHousekeepingServInnerServiceSMO;
import com.java110.intf.goods.IProductInnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.intf.user.IStoreV1InnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.DateUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Java110Cmd(serviceCode = "admin.queryAdminCount")
public class QueryAdminCountCmd extends Cmd {


    @Autowired
    private IStoreV1InnerServiceSMO storeInnerServiceSMOImpl;

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    @Autowired
    private IProductInnerServiceSMO productInnerServiceSMOImpl;

    @Autowired
    private IHousekeepingServInnerServiceSMO housekeepingServInnerServiceSMOImpl;

    @Autowired
    private IHousekeepingResourceInnerServiceSMO housekeepingResourceInnerServiceSMOImpl;

    @Autowired
    private IStoreOrderInnerServiceSMO storeOrderInnerServiceSMOImpl;


    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        String storeId = CmdContextUtils.getStoreId(context);

        StoreDto storeDto = new StoreDto();
        storeDto.setStoreId(storeId);
        storeDto.setStoreTypeCd(StoreDto.STORE_TYPE_ADMIN);
        int count = storeInnerServiceSMOImpl.queryStoresCount(storeDto);
        if (count < 1) {
            throw new CmdException("非法操作，请用系统管理员账户操作");
        }
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {

        List<Map> datas = new ArrayList<>();
        //todo 商铺个数

        ShopDto shopDto = new ShopDto();
        int shopCount = shopInnerServiceSMOImpl.queryShopsCount(shopDto);
        setDatas(datas, "店铺数", shopCount);

        //todo 商家个数
        StoreDto storeDto = new StoreDto();
        storeDto.setStoreTypeCd(StoreDto.STORE_TYPE_MALL);
        int storeCount = storeInnerServiceSMOImpl.queryStoresCount(storeDto);
        setDatas(datas, "商家数", storeCount);

        //todo 商品个数
        ProductDto productDto = new ProductDto();
        int productCount = productInnerServiceSMOImpl.queryProductsCount(productDto);
        setDatas(datas, "商品数", productCount);

        //todo 家政服务个数
        HousekeepingServDto housekeepingServDto = new HousekeepingServDto();
        int servCount = housekeepingServInnerServiceSMOImpl.queryHousekeepingServsCount(housekeepingServDto);
        setDatas(datas, "家政服务数", servCount);

        //todo 家政物品
        HousekeepingResourceDto housekeepingResourceDto = new HousekeepingResourceDto();
        int resourceCount = housekeepingResourceInnerServiceSMOImpl.queryHousekeepingResourcesCount(housekeepingResourceDto);
        setDatas(datas, "家政物品", resourceCount);


        Calendar startTimeCal = Calendar.getInstance();
        Calendar endTimeCal = Calendar.getInstance();
        //todo 今日销售订单
        startTimeCal.setTime(new Date());
        endTimeCal.setTime(new Date());
        StoreOrderDto storeOrderDto = new StoreOrderDto();
        storeOrderDto.setStartTime(DateUtil.getFormatTimeStringB(startTimeCal.getTime()));
        endTimeCal.add(Calendar.DAY_OF_MONTH,1);
        storeOrderDto.setEndTime(DateUtil.getFormatTimeStringA(endTimeCal.getTime()));
        int todayCount = storeOrderInnerServiceSMOImpl.queryReportStoreOrderCount(storeOrderDto);
        setDatas(datas, "今日订单", todayCount);

        double todayMoney = storeOrderInnerServiceSMOImpl.queryReportStoreOrderMoney(storeOrderDto);



        //todo 一周销售订单
        startTimeCal.setTime(new Date());
        endTimeCal.setTime(new Date());
        startTimeCal.add(Calendar.DAY_OF_MONTH,-7);
        storeOrderDto.setStartTime(DateUtil.getFormatTimeStringB(startTimeCal.getTime()));
        storeOrderDto.setEndTime(DateUtil.getFormatTimeStringA(endTimeCal.getTime()));
        int weekCount = storeOrderInnerServiceSMOImpl.queryReportStoreOrderCount(storeOrderDto);
        setDatas(datas, "周订单", weekCount);

        double weekMoney = storeOrderInnerServiceSMOImpl.queryReportStoreOrderMoney(storeOrderDto);

        //todo 当月销售订单
        startTimeCal.setTime(new Date());
        endTimeCal.setTime(new Date());
        startTimeCal.set(Calendar.DAY_OF_MONTH,1);
        storeOrderDto.setStartTime(DateUtil.getFormatTimeStringB(startTimeCal.getTime()));
        storeOrderDto.setEndTime(DateUtil.getFormatTimeStringA(endTimeCal.getTime()));
        int monthCount = storeOrderInnerServiceSMOImpl.queryReportStoreOrderCount(storeOrderDto);
        setDatas(datas, "月订单", monthCount);


        double monthMoney = storeOrderInnerServiceSMOImpl.queryReportStoreOrderMoney(storeOrderDto);

        //todo 今日销售金额
        setDatas(datas, "今日销售", todayMoney);

        //todo 一周销售金额
        setDatas(datas, "周销售", weekMoney);

        //todo 当月销售金额
        setDatas(datas, "月销售", monthMoney);


        context.setResponseEntity(ResultVo.createResponseEntity(datas));

    }

    private void setDatas(List<Map> datas, String name, int value) {
        Map info = new HashMap();
        info.put("name", name);
        info.put("value", value);
        datas.add(info);
    }

    private void setDatas(List<Map> datas, String name, double value) {
        Map info = new HashMap();
        info.put("name", name);
        info.put("value", value);
        datas.add(info);
    }
}
